﻿var cookies = function () {
    this.addCookie = function (name, value, overwrite) {
        var bl = this.checkExist(name);
        var cookieStr = document.cookie;
        if (bl == true) {
            if (overwrite == true || overwrite == undefined) {
                cookieStr = name + "=" + value;
                document.cookie = cookieStr;
            } else {
                var val = this.getCookie(name);
                val = val + "," + value;
                cookieStr = name + "=" + val;
                document.cookie = cookieStr;
            }
        } else {
            cookieStr = name + "=" + value;
            document.cookie = cookieStr;
        }
    }

    this.checkExist = function (name) {
        var bl = false;
        var cookieStr = document.cookie;
        var arrCookie = cookieStr.split(';');
        for (var i = 0; i < arrCookie.length; i++) {
            var arr = arrCookie[i].split("=");
            if (arr[0].trim() == name) {
                bl = true;
                break;
            }
        }
        return bl;
    }

    this.getKeys = function () {
        var cookieStr = document.cookie;
        var keys = [];
        if (cookieStr == "") {
            return
        }
        else {
            var arr = cookieStr.split(';');
            for (var i = 0; i < arr.length; i++) {
                keys.push(arr[i].split('=')[0]);
            }
        }
        return keys;
    }

    this.getValues = function () {
        var cookieStr = document.cookie;
        var vals = [];
        if (cookieStr == "") {
            return
        }
        else {
            var arr = cookieStr.split(';');
            for (var i = 0; i < arr.length; i++) {
                vals.push(arr[i].split('=')[1]);
            }
        }
        return vals;
    }

    this.getCookie = function (name) {
        var val = "";
        var cookieStr = document.cookie;
        var arrCookie = cookieStr.split(';');
        for (var i = 0; i < arrCookie.length; i++) {
            var arr = arrCookie[i].split("=");
            if (arr[0].trim() == name) {
                val = arr[1];
                break;
            }
        }
        return val;
    }

    this.delCookie = function (name) {
        var date = new Date();
        date.setTime(date.getTime() - 10000);
        document.cookie = name + "=" + this.getCookie(name) + "; expires=" + date.toGMTString();
    }
}